package com.project.service;

import com.project.common.utils.Constants;
import com.project.entity.MagFacultyInfo;
import com.project.entity.User;
import com.project.entity.UserRole;
import com.project.myBeans.MagfacultyInfoDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 14:09
 * @param:
 * @return:
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class MagFacultyServiceImpl extends BaseService implements MagFacultyService {
    @Autowired
    private UserService userService;

    @Autowired
    private MagFacultyInfoService magFacultyInfoService;

    @Autowired
    private UserRoleService userRoleService;


    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 根据userId删除行政人员账号
     *
     * @param userId
     * @return
     */
    @Override
    public boolean removeOneByUserId(Integer userId) {
        if (checkDeleteUpdateIsSuccess(userService.removeOneById(userId)) &&
                checkDeleteUpdateIsSuccess(magFacultyInfoService.removeOneByUserId(userId)))
            return true;
        else
            return false;
    }

    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 创建行政人员账号
     *
     * @param magfacultyInfoDetail
     * @return
     */
    @Override
    public boolean addOne(MagfacultyInfoDetail magfacultyInfoDetail) throws Exception {
        boolean flag = false;
        User user = magfacultyInfoDetail.getUser();
        MagFacultyInfo magFacultyInfo = magfacultyInfoDetail.getMagFacultyInfo();
        //先插入用户表
        int res = userService.createUser(user);
        //插入行政人员信息表
        magFacultyInfo.setUserid(user.getId());
        int res2 = magFacultyInfoService.add(magFacultyInfo);

        //用户权限表插入
        UserRole userRole = new UserRole();
        userRole.setUserid(user.getId());
        userRole.setRoleid(Constants.USERTYPE.MAGFACULTY.value());
        userRoleService.add(userRole);

        if (checkDeleteUpdateIsSuccess(res) && checkDeleteUpdateIsSuccess(res2)) flag = true;
        return flag;
    }

    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 根据用户表、行政人员信息表条件筛选
     *
     * @param magfacultyInfoDetail
     * @return
     */
    @Override
    public List<MagfacultyInfoDetail> findBatchByCondition(MagfacultyInfoDetail magfacultyInfoDetail) {

        return null;
    }
}
